package cn.labelnet.android.roomdb.user

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.GridLayoutManager
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import cn.labelnet.android.roomdb.R
import cn.labelnet.android.roomdb.base.data.DbHelper
import cn.labelnet.android.roomdb.base.data.tables.UserTable
import cn.labelnet.android.roomdb.user.adapter.UserListAdapter
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_user.*

/**
 * Created by yuan on 02/08/2017.
 * 用户列表 activity
 * 基本功能：
 * 添加用户 -> 编辑界面
 * 用户列表 -> 用户详情 -> 个人信息
 *                   -> 书籍列表 -> 添加书籍
 *                              -> 书籍详情
 *
 */
class UserListActivity : AppCompatActivity(), UserListAdapter.OnUserListListener {

    val userListAdapter = UserListAdapter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user)
        initView()
    }

    private fun initView() {
        val gridManager = GridLayoutManager(this, 2)
        gridManager.orientation = LinearLayout.VERTICAL
        rvUserList.layoutManager = gridManager
        rvUserList.setHasFixedSize(true)
        rvUserList.itemAnimator = DefaultItemAnimator()
        rvUserList.adapter = userListAdapter
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    /**
     * 加载数据
     */
    private fun loadData() {
        // 加载用户列表
        val observable = Observable.create(ObservableOnSubscribe<List<UserTable>> {
            e ->
            run {
                val selectUsers = DbHelper.INSTANCE!!.getUserDao().selectUsers()
                if (selectUsers.isEmpty()) {
                    e.onError(Throwable("no data"))
                } else {
                    e.onNext(selectUsers)
                }
            }
        })

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ datas ->
                    run {
                        showData()
                        userListAdapter.clearUsers()
                        userListAdapter.addUsers(datas)
                    }
                }, {
                    e ->
                    run {
                        hideData()
                        Log.v("User", "error msg : ${e.message}")
                    }
                })
    }


    /*
     * 有数据
     */
    private fun showData() {
        rvUserList.visibility = View.VISIBLE
        tvNoData.visibility = View.GONE
    }

    /**
     * 无数据
     */
    private fun hideData() {
        rvUserList.visibility = View.GONE
        tvNoData.visibility = View.VISIBLE
    }

    override fun onItemClick(user: UserTable) {
        // 点击事件
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item != null) {
            if (item.itemId == R.id.addUser) {
                // add user
                startActivity(Intent(this, UserAddPageActivity::class.java))
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        MenuInflater(this).inflate(R.menu.menu_user_list, menu)
        return super.onCreateOptionsMenu(menu)
    }
}